/* 
THIS FILE IMPORTS THE RAW DATA OF:
- CONSUMER CONFIDENCE INDEX
- BUSINESS CONFIDENCE INDEX
- LEADING INDICATORS

BECAUSE THE STRUCTURE IS SIMILAR FOR EACH VARIABLE WE ONLY COMMENT THE CONSUMER CONFIDENCE INDEX CODE SECTION
*/

*************************************************************************
*** CONSUMER CONFIDENCE INDEX
import delimited "$datapath/confidence/DP_LIVE_09082022153223549", clear
rename value cci // named the variable "cci"

* date: nice monthly format
gen datem = mofd(date(time,"YM",2050))
format datem %tm

* country name
rename ïlocation country

* drop economic zones
drop if country == "OECD"
drop if country == "EA19"
drop if country == "G-7"
drop if country == "G7M"
drop if country == "OECDE"
drop if country == "OEU"
drop if country == "LUX"

* use full country names to match with the EPFR data
replace country = "AUSTRALIA" if country == "AUS"
replace country = "AUSTRIA" if country == "AUT"
replace country = "BELGIUM" if country == "BEL"
replace country = "BRAZIL" if country == "BRA"
replace country = "CANADA" if country == "CAN"
replace country = "SWITZERLAND" if country == "CHE"
replace country = "CHILE" if country == "CHL"
replace country = "CHINA" if country == "CHN"
replace country = "COLOMBIA" if country == "COL"
replace country = "CZECHREPUBLIC" if country == "CZE"
replace country = "GERMANY" if country == "DEU"
replace country = "DENMARK" if country == "DNK"
replace country = "SPAIN" if country == "ESP"
replace country = "ESTONIA" if country == "EST"
replace country = "FINLAND" if country == "FIN"
replace country = "FRANCE" if country == "FRA"
replace country = "UNITEDKINGDOM" if country == "GBR"
replace country = "GREECE" if country == "GRC"
replace country = "HUNGARY" if country == "HUN"
replace country = "INDIA" if country == "IND"
replace country = "INDONESIA" if country == "IDN"
replace country = "IRLAND" if country == "IRL"
replace country = "ISRAEL" if country == "ISR"
replace country = "ITALY" if country == "ITA"
replace country = "JAPAN" if country == "JPN"
replace country = "KOREASOUTH" if country == "KOR"
replace country = "LITHUANIA" if country == "LTU"
replace country = "MEXICO" if country == "MEX"
replace country = "NEDERLANDS" if country == "NLD"
replace country = "NEWZEALAND" if country == "NZL"
replace country = "POLAND" if country == "POL"
replace country = "PORTUGAL" if country == "PRT"
replace country = "RUSSIA" if country == "RUS"
replace country = "SLOVAKIA" if country == "SVK"
replace country = "SLOVENIA" if country == "SVN"
replace country = "SWEDEN" if country == "SWE"
replace country = "TURKEY" if country == "TUR"
replace country = "UNITEDSTATES" if country == "USA"
replace country = "SOUTHAFRICA" if country == "ZAF"

* keep key variables
keep country datem cci
replace country = lower(country) // UPPERCASE  to lowercase

* temporarily save data
tempfile cci
save `cci', replace
*************************************************************************

*************************************************************************
* BUSINESS CONFIDENCE INDEX
import delimited "$datapath/confidence/DP_LIVE_09082022153414987", clear
rename value bci

gen datem = mofd(date(time,"YM",2050))
format datem %tm

rename ïlocation country

drop if country == "OECD"
drop if country == "EA19"
drop if country == "G-7"
drop if country == "G7M"
drop if country == "OECDE"
drop if country == "OEU"
drop if country == "LUX"


replace country = "AUSTRALIA" if country == "AUS"
replace country = "AUSTRIA" if country == "AUT"
replace country = "BELGIUM" if country == "BEL"
replace country = "BRAZIL" if country == "BRA"
replace country = "CANADA" if country == "CAN"
replace country = "SWITZERLAND" if country == "CHE"
replace country = "CHILE" if country == "CHL"
replace country = "CHINA" if country == "CHN"
replace country = "COLOMBIA" if country == "COL"

replace country = "CZECHREPUBLIC" if country == "CZE"
replace country = "GERMANY" if country == "DEU"
replace country = "DENMARK" if country == "DNK"
replace country = "SPAIN" if country == "ESP"
replace country = "ESTONIA" if country == "EST"

replace country = "FINLAND" if country == "FIN"
replace country = "FRANCE" if country == "FRA"
replace country = "UNITEDKINGDOM" if country == "GBR"

replace country = "GREECE" if country == "GRC"
replace country = "HUNGARY" if country == "HUN"
replace country = "INDIA" if country == "IND"
replace country = "INDONESIA" if country == "IDN"
replace country = "IRLAND" if country == "IRL"
replace country = "ISRAEL" if country == "ISR"

replace country = "ITALY" if country == "ITA"
replace country = "JAPAN" if country == "JPN"
replace country = "KOREASOUTH" if country == "KOR"
replace country = "LITHUANIA" if country == "LTU"

replace country = "MEXICO" if country == "MEX"
replace country = "NEDERLANDS" if country == "NLD"
replace country = "NEWZEALAND" if country == "NZL"
replace country = "POLAND" if country == "POL"

replace country = "PORTUGAL" if country == "PRT"
replace country = "RUSSIA" if country == "RUS"
replace country = "SLOVAKIA" if country == "SVK"
replace country = "SLOVENIA" if country == "SVN"
replace country = "SWEDEN" if country == "SWE"
replace country = "TURKEY" if country == "TUR"
replace country = "UNITEDSTATES" if country == "USA"
replace country = "SOUTHAFRICA" if country == "ZAF"

keep country datem bci
replace country = lower(country)

tempfile bci
save `bci', replace
*************************************************************************

*************************************************************************
* LEADING INDICATOR
import delimited "$datapath/confidence/DP_LIVE_09082022153603283", clear
rename value cli

gen datem = mofd(date(time,"YM",2050))
format datem %tm

rename ïlocation country

drop if country == "OECD"
drop if country == "EA19"
drop if country == "G-7"
drop if country == "G7M"
drop if country == "OECDE"
drop if country == "OEU"
drop if country == "LUX"


replace country = "AUSTRALIA" if country == "AUS"
replace country = "AUSTRIA" if country == "AUT"
replace country = "BELGIUM" if country == "BEL"
replace country = "BRAZIL" if country == "BRA"
replace country = "CANADA" if country == "CAN"
replace country = "SWITZERLAND" if country == "CHE"
replace country = "CHILE" if country == "CHL"
replace country = "CHINA" if country == "CHN"
replace country = "COLOMBIA" if country == "COL"

replace country = "CZECHREPUBLIC" if country == "CZE"
replace country = "GERMANY" if country == "DEU"
replace country = "DENMARK" if country == "DNK"
replace country = "SPAIN" if country == "ESP"
replace country = "ESTONIA" if country == "EST"

replace country = "FINLAND" if country == "FIN"
replace country = "FRANCE" if country == "FRA"
replace country = "UNITEDKINGDOM" if country == "GBR"

replace country = "GREECE" if country == "GRC"
replace country = "HUNGARY" if country == "HUN"
replace country = "INDIA" if country == "IND"
replace country = "INDONESIA" if country == "IDN"
replace country = "IRELAND" if country == "IRL"
replace country = "ISRAEL" if country == "ISR"

replace country = "ITALY" if country == "ITA"
replace country = "JAPAN" if country == "JPN"
replace country = "KOREASOUTH" if country == "KOR"
replace country = "LITHUANIA" if country == "LTU"

replace country = "MEXICO" if country == "MEX"
replace country = "NETHERLANDS" if country == "NLD"
replace country = "NEWZEALAND" if country == "NZL"
replace country = "POLAND" if country == "POL"
replace country = "NORWAY" if country == "NOR"

replace country = "PORTUGAL" if country == "PRT"
replace country = "RUSSIA" if country == "RUS"
replace country = "SLOVAKIA" if country == "SVK"
replace country = "SLOVENIA" if country == "SVN"
replace country = "SWEDEN" if country == "SWE"
replace country = "TURKEY" if country == "TUR"
replace country = "UNITEDSTATES" if country == "USA"
replace country = "SOUTHAFRICA" if country == "ZAF"

keep country datem cli
replace country = lower(country)

tempfile cli
save `cli', replace
*************************************************************************

*************************************************************************
* MERGE TEMPORARY CLEANED DATA 
use `cli', clear
merge 1:1 country datem using `cci', nogenerate
merge 1:1 country datem using `bci', nogenerate

* lagged values
bys country (datem) : gen lcci = cci[_n-1]
bys country (datem) : gen lbci = bci[_n-1]
bys country (datem) : gen lcli = cli[_n-1]

* first difference
gen dcci = cci - lcci
gen dbci = bci - lbci
gen dcli = cli - lcli

* save final data
save "$datapath/confidence", replace

*end
*************************************************************************